package com.yzg.juc;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author misterWei
 * @create 2020年07月11号:18点29分
 * @mailbox forcontinue@163.com
 *
 * 协调多个线程计数,达到某个顶峰全部释放
 */
@Slf4j(topic = "c.CyclicBarrierTest")
public class CyclicBarrierTest {


    @SneakyThrows
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

        new Thread(()->{
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            log.info("释放了!");
        },"t1").start();
        Thread.sleep(2000L);
        new Thread(()->{
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            log.info("释放了!");
        },"t2").start();


    }



}
